﻿#include "Enemy.h"
#include<qdebug.h>
#include <qrandom.h>
#include "EnemyPlane.h"
#include"WeaponBank.h"
Enemy::Enemy()
{
	init();
}
Enemy::~Enemy()
{
	if (m_WeaponBank != nullptr)
	{
		delete m_WeaponBank;
		m_WeaponBank = nullptr;
	}
}
void Enemy::init()
{
	m_Property[EnemyPlaneProperty::Type::small] = EnemyPlaneProperty("小飞机正常", "小飞机死亡",1500, 3000, 150, 5,10);
	m_Property[EnemyPlaneProperty::Type::middle] = EnemyPlaneProperty("中飞机正常", "中飞机死亡",3000, 5000, 80, 10,20);
	m_Property[EnemyPlaneProperty::Type::big] = EnemyPlaneProperty("大飞机正常", "大飞机死亡",20000, 40000, 30, 20,40);
	
}

void Enemy::addPlane(const QPoint& point, const EnemyPlaneProperty::Type type)
{
	//qDebug() << "创造飞机";
	m_Enemy.append(new EnemyPlane(point));
	EnemyPlane& plane= *m_Enemy.back();
	//qDebug() << "设置飞机";
	//设置一些基本属性
	plane.addFrame(m_Property[type].ModelStand,"正常");
	plane.getModel()["正常"].setDelayTime(300);
	plane.addFrame(m_Property[type].ModelDeath, "死亡");
	plane.getModel()["死亡"].setDelayTime(500);
	plane.setWindow(getWindow());
	plane.setProperty(QVector2D(0, 1), m_Property[type].Steplen);
	plane.setHp(m_Property[type].Hp);
	plane.setMaxHp(m_Property[type].Hp);
	plane.setScore(m_Property[type].score);
	plane.creatorWeaponBank(m_WeaponBank);
	//plane.getWeaponBank()->setProperty(QVector2D(0, 1), 200);
	//plane.getWeaponBank()->setBulletTimer(1);
	//plane.getWeaponBank()->setBulletFrame("敌机子弹");
	plane.setAutoBullet(m_Property[type].AutoBullet, m_Property[type].AutoBulletTime);
	//开启血条
	if(m_Property[type].flagBlood)
	plane.startBlood(m_Property[type].space, m_Property[type].dir);
	//qDebug() << getCollider();
	//qDebug() << plane.getWeaponBank()->getWindowSize();
	plane.creatorWeaponBank();
	//qDebug() << plane.getWeaponBank()->getWindowSize();
	
}

void Enemy::addPlane(const int x, const int y, const EnemyPlaneProperty::Type type)
{
	addPlane(QPoint(x,y),type);
}

void Enemy::setTimerPlane(const int MinTime, const int MaxTime, const EnemyPlaneProperty::Type type)
{
	if (MinTime > MaxTime)
	{
		OutInfo;
		qDebug() << "最小值大于最大值";
	}
	m_Property[type].MaxTime = MaxTime;
	m_Property[type].MinTime = MinTime;
}
void Enemy::setSteplenPlane(const int Steplen, const EnemyPlaneProperty::Type type)
{
	m_Property[type].Steplen = Steplen;
}
void Enemy::setHpPlane(const int hp, const EnemyPlaneProperty::Type type )
{
	m_Property[type].Hp = hp;
}
void Enemy::setAutoBullet(bool flag,int time, const EnemyPlaneProperty::Type type)
{
	m_Property[type].AutoBullet = flag;
	m_Property[type].AutoBulletTime = time;
	if(flag)
	creatorWeaponBank();
}
void Enemy::setBlood(EnemyPlaneProperty& plan,bool flagBlood, int space, const Plane::Direction& dir)
{
	plan.flagBlood = flagBlood;
	if (space == -1)
		return;
	plan.space = space;
	plan.dir = dir;
}
void Enemy::startBlood(int space, const Plane::Direction& dir, const EnemyPlaneProperty::Type type)
{
	setBlood(m_Property[type],true,space,dir);
}
void Enemy::startBlood(int space, const Plane::Direction& dir)
{
	for (auto&v: m_Property)
	{
		setBlood(v,true, space, dir);
	}
}
void Enemy::closeBlood(const EnemyPlaneProperty::Type type)
{
	setBlood(m_Property[type],false,-1, Plane::Direction::down);
}
void Enemy::closeBlood()
{
	for (auto& v : m_Property)
	{
		setBlood(v, false, -1, Plane::Direction::down);
	}
}
void Enemy::setPlaneType(const int type)
{
	m_PlaneType = type;
	startAuto();
}
void Enemy::setWindow(const QRect& rect)
{
	Articles::setWindow(rect);
	for (auto it = m_Property.begin(); it != m_Property.end(); it++)
	{
		//设置敌机随机出现的范围
		it->maxRight = getWindow().width() - getFrame(it->ModelStand)->begin()->width();
	}
}
void Enemy::setCollider(const QRect& collider)
{
	Articles::setCollider(collider);
	
}
void Enemy::update()
{
	//数据更新
	for (auto it = m_Enemy.begin(); it != m_Enemy.end(); it++)
	{
		(*it)->update();
		(*it)->AutoMove();
		if (!(*it)->isActive())
		{
			//qDebug() << "释放一个飞机:"<< m_Enemy.count();
			delete (*it);
			it = m_Enemy.erase(it);//释放不是活动的飞机
			if (it == m_Enemy.end())
				break;
		}
	}
	if (m_WeaponBank != nullptr)
		m_WeaponBank->update();
}

QVector<EnemyPlane*>& Enemy::getEnemyPlane()
{
	return m_Enemy;
}

void Enemy::setPause(bool flag)
{
	Articles::setPause(flag);
	for (auto& v : m_Enemy)
	{
		v->setPause(flag);
	}
	if(flag)
		closeAuto();
	else
		startAuto();
}

void Enemy::draw(QPainter* painter)
{
	for (auto& v : m_Enemy)
	{
		v->draw(painter);
	}
	if (m_WeaponBank != nullptr)
	{
		m_WeaponBank->draw(painter);
	}
}

void Enemy::startAuto()
{
	//定时器自动放飞机函数设置
	AutoPlane();
	for (auto it = m_Property.begin(); it != m_Property.end(); it++)
	{
		if (it.key() & m_PlaneType)
		{
			it->UpTime();
			//it->Timer.start();
		}
		else
		{
			it->Timer.stop();
		}
	}
}

void Enemy::closeAuto()
{
	for (auto& v : m_Property)
	{
		v.Timer.stop();
	}
}

void Enemy::creatorWeaponBank()
{
	if (m_WeaponBank == nullptr)
	{
		if (getWindowSize().isEmpty())
		{
			OutInfo;
			qDebug() << "请先设置setWindowSize()";
			return;
		}
		m_WeaponBank = new WeaponBank(100, m_parent);
		m_WeaponBank->setWindowSize(getWindowSize());
		m_WeaponBank->setProperty(QVector2D(0, 1), 200);
		m_WeaponBank->setBulletTimer(1);
		m_WeaponBank->setBulletFrame("敌机子弹");
	}
}

WeaponBank* Enemy::getWeaponBank()
{
	return m_WeaponBank;
}

void Enemy::AutoPlane()
{
	if (getWindow().isNull())
	{
		OutInfo;
		qDebug() << "请在外部调用setWindow,缺少重要数据，程序无法继续运行,无法放出敌机";
		return;
	}
	for (auto it = m_Property.begin(); flag&& it != m_Property.end(); it++)
	{
		it->Timer.callOnTimeout([=]() {
			//qDebug() << 0<< it->maxRight;
			int x = random(0, it->maxRight);
			int y = -getFrame(it->ModelStand)->begin()->height() * 0.9;
			//qDebug() << x << "," << y;
			addPlane(x, y, EnemyPlaneProperty::Type(it.key()));
			it->setUpTime();
			});	
	}
	flag = false;
}
